//-----------------------------------------------------------------------
// <copyright file="FeedbackWorkaroundPresenter.cs" company="Simnova LLC">
//     Copyright (c) Simnova LLC. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace Simnova.Collaborate.Presenter
{
    using System;
    using System.Collections;
    using System.Text;
    using Simnova.Collaborate.View;

    public class FeedbackWorkaroundPresenter
    {
        private IFeedbackWorkaround feedbackWorkaroundView;
        private int userId;
        private int feedbackId;

        public FeedbackWorkaroundPresenter(IFeedbackWorkaround feedbackWorkaroundView, int feedbackId, int userId)
        {
            this.feedbackWorkaroundView = feedbackWorkaroundView;
            this.userId = userId;
            this.feedbackId = feedbackId;
            AppServer.CollaborateServiceClient collaborateClient = new AppServer.CollaborateServiceClient();
            AppServer.Feedback feedback = collaborateClient.GetFeedback(this.feedbackId);
            collaborateClient.Close();
            this.feedbackWorkaroundView.Title = feedback.Title;
            this.feedbackWorkaroundView.Description = feedback.Description;
            this.LoadWorkaround();
            this.LoadWorkarounds();
        }

        public void Save()
        {
            AppServer.CollaborateServiceClient collaborateClient = new AppServer.CollaborateServiceClient();
            AppServer.FeedbackWorkaround feedbackWorkaround = collaborateClient.GetFeedbackWorkaroundByFeedback(this.userId, this.feedbackId);
            if (feedbackWorkaround == null)
            {
                feedbackWorkaround = new AppServer.FeedbackWorkaround();
                feedbackWorkaround.UserId = this.userId;
                feedbackWorkaround.FeedbackId = this.feedbackId; 
            }

            feedbackWorkaround.Workaround = this.feedbackWorkaroundView.Workaround;
            collaborateClient.SaveFeedbackWorkaround(feedbackWorkaround);
            collaborateClient.Close();
            this.LoadWorkaround();
            this.LoadWorkarounds();
        }

        private void LoadWorkarounds()
        {
            AppServer.CollaborateServiceClient collaborateClient = new AppServer.CollaborateServiceClient();
            this.feedbackWorkaroundView.Workarounds = collaborateClient.GetFeedbackWorkarounds(this.feedbackId);
            collaborateClient.Close();
        }

        private void LoadWorkaround()
        {
            AppServer.CollaborateServiceClient collaborateClient = new AppServer.CollaborateServiceClient();
            AppServer.FeedbackWorkaround feedbackWorkaround = collaborateClient.GetFeedbackWorkaroundByFeedback(this.userId, this.feedbackId);
            collaborateClient.Close();
            if (feedbackWorkaround == null)
            {
                this.feedbackWorkaroundView.Workaround = "";
            }
            else
            {
                this.feedbackWorkaroundView.Workaround = feedbackWorkaround.Workaround;
            }
        }
    }
}
